package com.bjsxt.shop.users.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.shop.common.util.ConstatFinalUtil;
import com.bjsxt.shop.common.util.PageInfoUtil;
import com.bjsxt.shop.users.dao.IAAdminsDao;
import com.bjsxt.shop.users.dao.IARoleDao;
import com.bjsxt.shop.users.pojo.AAdmins;
import com.bjsxt.shop.users.pojo.ARole;
import com.bjsxt.shop.users.service.IUsersService;

@Service("usersService")
public class UsersServiceImpl implements IUsersService
{
	@Resource
	private IAAdminsDao adminsDao;
	@Resource
	private IARoleDao roleDao;

	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condmap)
	{
		return this.adminsDao.findOne(condmap);
	}

	@Override
	public JSONObject saveOneAdminsService(AAdmins admin) {
		int res = this.adminsDao.save(admin);
		JSONObject resultjson = new JSONObject();
		
		if(res>0)
		{
			resultjson.put("code", "0");
			resultjson.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			JSONObject datajson = new JSONObject();
			datajson.put("id", admin.getId());
			datajson.put("affect",res+"");
			resultjson.put("data", datajson);
			
		}else
		{
			resultjson.put("code", "1");
			resultjson.put("1", ConstatFinalUtil.INFO_JSON.get("1"));
			
			
		}
		return resultjson;
	}

	@Override
	public JSONObject deleteOneAdminsService(AAdmins admin)
	{
		JSONObject resultjson = new JSONObject();
		int res = adminsDao.delete(admin);
		if(res>0)
		{
			resultjson.put("code", "0");
			resultjson.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
		} else
		{
			resultjson.put("code", "3");
			resultjson.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
		}
		return resultjson;
	}

	@Override
	public JSONObject updataOneAdminsService(AAdmins admin)
	{
		
		int res = adminsDao.update(admin);
		JSONObject resultjson = new JSONObject();
		if(res>0)
		{
			resultjson .put("code", "0");
			resultjson.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultjson.put("code", "3");
			resultjson.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}
		
		return resultjson;
	}

	@Override
	public List<AAdmins> findListAdminsService(PageInfoUtil pageinfo, Map<String, Object> condmap)
	{
		if(condmap.get("keyword")!=null&&!"".equals(condmap.get("keyword")+""))
		{
			condmap.put("keyword", "%"+condmap.get("keyword")+"%");
		}
		if(pageinfo != null)
		{
			/* 分页 */
			/* 查询总记录数 */
			condmap.put("pageCount", "true");
			List<AAdmins> roleList = this.adminsDao.findCondList(condmap);
			/* 取总记录数 */
			if(roleList.size() == 1)
			{
				pageinfo.setTotalRecord(roleList.get(0).getId());
			}
			
			/* 查询数据 */
			condmap.put("pageCount", "false");
			/* limit ? , ? */
			condmap.put("currentRecord", pageinfo.getCurrentRecord());
			condmap.put("pageSize", pageinfo.getPageSize());
			return this.adminsDao.findCondList(condmap);
		}
		/* 不分页查询数据 */
		return this.adminsDao.findCondList(condmap);
		
	}
	/*------------------------角色----------------------*/
	@Override
	public ARole findOnerolesService(Map<String, Object> condmap)
	{
		return this.roleDao.findOne(condmap);
	}

	@Override
	public JSONObject saveOnerolesService(ARole role) {
		int res = this.roleDao.save(role);
		JSONObject resultjson = new JSONObject();
		
		if(res>0)
		{
			resultjson.put("code", "0");
			resultjson.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			JSONObject datajson = new JSONObject();
			datajson.put("id", role.getId());
			datajson.put("affect",res+"");
			resultjson.put("data", datajson);
			
		}else
		{
			resultjson.put("code", "1");
			resultjson.put("1", ConstatFinalUtil.INFO_JSON.get("1"));
			
			
		}
		return resultjson;
	}

	@Override
	public JSONObject deleteOnerolesService(ARole role)
	{
		JSONObject resultjson = new JSONObject();
		int res = roleDao.delete(role);
		if(res>0)
		{
			resultjson.put("code", "0");
			resultjson.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
		} else
		{
			resultjson.put("code", "3");
			resultjson.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
		}
		return resultjson;
	}

	@Override
	public JSONObject updataOnerolesService(ARole role)
	{
		
		int res = roleDao.update(role);
		JSONObject resultjson = new JSONObject();
		if(res>0)
		{
			resultjson .put("code", "0");
			resultjson.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}else
		{
			resultjson.put("code", "3");
			resultjson.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		}
		
		return resultjson;
	}

	@Override
	public List<ARole> findListrolesService(PageInfoUtil pageinfo, Map<String, Object> condmap)
	{
		if(condmap.get("keyword")!=null&&!"".equals(condmap.get("keyword")+""))
		{
			condmap.put("keyword", "%"+condmap.get("keyword")+"%");
		}
		if(pageinfo != null)
		{
			/* 分页 */
			/* 查询总记录数 */
			condmap.put("pageCount", "true");
			List<ARole> roleList = this.roleDao.findCondList(condmap);
			/* 取总记录数 */
			if(roleList.size() == 1)
			{
				pageinfo.setTotalRecord(roleList.get(0).getId());
			}
			
			/* 查询数据 */
			condmap.put("pageCount", "false");
			/* limit ? , ? */
			condmap.put("currentRecord", pageinfo.getCurrentRecord());
			condmap.put("pageSize", pageinfo.getPageSize());
			return this.roleDao.findCondList(condmap);
		}
		/* 不分页查询数据 */
		return this.roleDao.findCondList(condmap);
		
	}

}
